<?php
namespace zake\app\help;


class httpEncode{


    /**
     * 解析http请求类型和url
     * @param $headerBuffer
     * @return array|null
     *
     * @author liu.bin 2020-09-04 14:57
     */
    public static function parseFirstLine($headerBuffer){
        if(empty($headerBuffer)){
            return null;
        }
        list($firstLine,) = explode("\r\n",$headerBuffer,2);
        list($method,$url,$httpVersion) = explode(" ",$firstLine);
        return [$method,$url];
    }


    /**
     * 解析当前url，映射成本地文件
     * @param string $url
     * @return string 文件数据
     * @author liu.bin 2020-09-03 19:02
     */
    public static function localFile($url){


        if(strpos($url,"?")){
            list($filePath,) = explode("?",$url,2);
        }else{
            $filePath = $url;
        }
        $filePath = ROOT_PATH . $filePath;
        if(!file_exists($filePath)){
            \zake\helper\Console::success("[http]-[".$filePath."]");
            return self::code404();
        }

        $content = file_get_contents($filePath);
        $pathInfo = pathinfo($filePath);
        switch ($pathInfo['extension']){
            case "html":
                return self::html($content);
            case "css":
                return self::css($content);
            case "js":
                return self::js($content);
            case "jpg":
                return self::jpg($content);
            default:
                return self::default($content);
        }

    }



    private static function html($content){
        // 返回浏览器
        $httpBuffer = "HTTP/1.1 200 OK\r\n";
        $httpBuffer .= "Server: zake/0.9.0\r\n";
        $httpBuffer .= "Content-Type: text/html;charset=utf-8\r\n";
        $httpBuffer .= "Connection: close\r\n";
        $httpBuffer .= "Content-Length: " . strlen($content) . "\r\n\r\n";
        $httpBuffer .= $content;
        return $httpBuffer;
    }



    private static function css($content){

        // 返回浏览器
        $httpBuffer = "HTTP/1.1 200 OK\r\n";
        $httpBuffer .= "Server: zake/0.9.0\r\n";
        $httpBuffer .= "Content-Type: text/css\r\n";
        $httpBuffer .= "Connection: close\r\n";
        $httpBuffer .= "Content-Length: " . strlen($content) . "\r\n\r\n";
        $httpBuffer .= $content;
        return $httpBuffer;
    }



    private static function js($content){

        // 返回浏览器
        $httpBuffer = "HTTP/1.1 200 OK\r\n";
        $httpBuffer .= "Server: zake/0.9.0\r\n";
        $httpBuffer .= "Content-Type: text/javascript\r\n";
        $httpBuffer .= "Connection: close\r\n";
        $httpBuffer .= "Content-Length: " . strlen($content) . "\r\n\r\n";
        $httpBuffer .= $content;
        return $httpBuffer;
    }



    private static function jpg($content){
        // 返回浏览器
        $httpBuffer = "HTTP/1.1 200 OK\r\n";
        $httpBuffer .= "Server: zake/0.9.0\r\n";
        $httpBuffer .= "Content-Type: image/jpg\r\n";
        $httpBuffer .= "Connection: close\r\n";
        $httpBuffer .= "Content-Length: " . strlen($content) . "\r\n\r\n";
        $httpBuffer .= $content;
        return $httpBuffer;
    }


    private static function default($content){
        // 返回浏览器
        $httpBuffer = "HTTP/1.1 200 OK\r\n";
        $httpBuffer .= "Server: zake/0.9.0\r\n";
        $httpBuffer .= "Connection: close\r\n";
        $httpBuffer .= "Content-Length: " . strlen($content) . "\r\n\r\n";
        $httpBuffer .= $content;
        return $httpBuffer;
    }


    public static function code404(){
        // 返回浏览器
        $httpBuffer = "HTTP/1.1 404 OK\r\n";
        $httpBuffer .= "Server: zake/0.9.0\r\n";
        $httpBuffer .= "Content-Type: text/html;charset=utf-8\r\n";
        $httpBuffer .= "Connection: close\r\n";

        return $httpBuffer;

    }

}